﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CocisDAL;
using CocisModel;
using CocisModel.Common;
using System.Data;
using CocisWeb.src;

namespace CocisWeb.ManageAsset
{
    public partial class AddDatabaseInfo : BasePage
    {
        public AssetDAL dal = new AssetDAL();
        public LogsDAL logs = new LogsDAL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this.BindDropDownList();
                this.txtDBCreatedDate.Text = DateTime.Now.ToShortDateString();
            }
        }
        public void BindDropDownList()
        {

            this.ddlFullInstanceName.DataSource = dal.GetDBInstance();
            this.ddlFullInstanceName.DataTextField = "FullInstanceName";
            this.ddlFullInstanceName.DataValueField = "DBInstanceID";
            this.ddlFullInstanceName.DataBind();

            this.ddlStatus.DataSource = dal.GetStatus();
            this.ddlStatus.DataTextField = "Status";
            this.ddlStatus.DataValueField = "StatusID";
            this.ddlStatus.DataBind();

            this.ddlCVCoverage.DataSource = dal.GetCVCoverage();
            this.ddlCVCoverage.DataTextField = "CVCoverage";
            this.ddlCVCoverage.DataValueField = "CVCoverageID";
            this.ddlCVCoverage.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Asset.DatabaseInfoDataTable dt = new Asset.DatabaseInfoDataTable();
            Asset.DatabaseInfoRow rows = dt.NewDatabaseInfoRow();
            Guid gd = new Guid();
            rows.DatabaseID = gd;
            rows.DBInstanceID = this.ddlFullInstanceName.SelectedValue.ToString();
            rows.DBName = this.txtDBName.Text;
            rows.DBSize = this.txtDBSize.Text;
            rows.UnallocatedSpace = this.txtUnallocatedSpace.Text;
            rows.LogSize = this.txtLogSize.Text;
            rows.LogSpaceUsedPercent = this.txtLogSpaceUsedPercent.Text;
            rows.DataAutogrowth = this.txtDataAutogrowth.Text;
            rows.LogAutogrowth = this.txtLogAutogrowth.Text;
            rows.StatusID =int.Parse(this.ddlStatus.SelectedValue.ToString());
            rows.RecoveryModel = this.txtRecoveryModel.Text;
            rows.DBOwner = this.txtDBOwner.Text;
            rows.DBCreated = DateTime.Parse(this.txtDBCreatedDate.Text);
            rows.CVCoverageID = int.Parse(this.ddlCVCoverage.SelectedValue.ToString());
            rows.Application = this.txtApplication.Text;
            rows.DBSupportTeam = this.txtDBSupportTeam.Text;
           
            dt.Rows.Add(rows);
            dt.AcceptChanges();

            int temp = dal.AddDatabaseInfo(dt);
            if (temp > 0)
            {
                string ip = Request.UserHostAddress.ToString();
                int t = logs.AddNewLogsInfo("insert new DatabaseInfo", LoginUser.UserName, ip);

                ClientScript.RegisterStartupScript(this.GetType(), "a", "<script language=javascript>alert('添加成功！')</script>");
             
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "b", "<script language=javascript>alert('添加失败！')</script>");

            }


        }
       
    }
}
